package com.jichaoyun.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jichaoyun.common.enums.MyExceptiontType;
import com.jichaoyun.common.ex.MyException;
import com.jichaoyun.common.model.PagedResult;
import com.jichaoyun.common.model.Result;
import com.jichaoyun.model.entity.*;
import com.jichaoyun.model.vo.LimitAlarmVo;
import com.jichaoyun.model.vo.AlarmVo;
import com.jichaoyun.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@Api(tags = {"报警信息接口"})
@RequestMapping("/sys/AlarmInfo")
public class AlarmController {

    @Autowired
    private PowerAlarmService powerAlarmService;

    @Autowired
    private LimitAlarmService limitAlarmService;

    @Autowired
    private DcAlarmService dcAlarmService;

    @Autowired
    private DoorOpenAlarmService doorOpenAlarmService;

    @Autowired
    private ComErrAlarmService comErrAlarmService;

    @Autowired
    private ParaAlarmService paraAlarmService;

    @Autowired
    private IBalanceAlarmService balanceAlarmService;

    @Autowired
    private ValveAlarmService valveAlarmService;

    @Autowired
    private  PipelossAlarmService pipelossAlarmService;

    @ApiOperation("报警通知，轮询接口")
    @GetMapping("/getStationAlarmInfo")
    public Result<List<Map<String,Object>>> getStationAlarmInfo(Integer userId) {
        List<Map<String,Object>> resList = new ArrayList<>();
        resList.addAll(comErrAlarmService.pollingDb(userId));
        resList.addAll(dcAlarmService.pollingDb(userId));
        resList.addAll(doorOpenAlarmService.pollingDb(userId));
        resList.addAll(balanceAlarmService.pollingDb(userId));
        resList.addAll(limitAlarmService.pollingDb(userId));
        resList.addAll(paraAlarmService.pollingDb(userId));
        resList.addAll(powerAlarmService.pollingDb(userId));
        resList.addAll(valveAlarmService.pollingDb(userId));
        return Result.success(resList);
    }


    @ApiOperation(value = "报警通知，报警信息已读接口")
    @PostMapping("/updateAlarmRead")
    public Result<Void> updateAlarmRead(Integer id,String alarmInfo,Integer userId) {

        switch (alarmInfo){
            case "通讯报警":{
                comErrAlarmService.MakeAlarmRead(id,userId);
                break;
            }
            case "直流报警":{
                dcAlarmService.MakeAlarmRead(id,userId);break;
            }
            case "门开报警":{
                doorOpenAlarmService.MakeAlarmRead(id,userId);break;
            }
            case "余额报警":{
                balanceAlarmService.MakeAlarmRead(id,userId);break;
            }
            case "超限报警":{
                limitAlarmService.MakeAlarmRead(id,userId);break;
            }
            case "仪表组态报警":{
                paraAlarmService.MakeAlarmRead(id,userId);break;
            }
            case "断电报警":{
                powerAlarmService.MakeAlarmRead(id,userId);break;
            }
            case "阀门报警":{
                valveAlarmService.MakeAlarmRead(id,userId);break;
            }
        }
        return Result.success(null);
    }
    @ApiOperation(value = "报警通知，报警信息全部已读已读接口")
    @PostMapping("/updateAlarmReadByUserId")
    public Result<Void> updateAllAlarmRead(Integer userId) {
        comErrAlarmService.MakeAlarmRead(userId);
        dcAlarmService.MakeAlarmRead(userId);
        doorOpenAlarmService.MakeAlarmRead(userId);
        balanceAlarmService.MakeAlarmRead(userId);
        limitAlarmService.MakeAlarmRead(userId);
        paraAlarmService.MakeAlarmRead(userId);
        powerAlarmService.MakeAlarmRead(userId);
        valveAlarmService.MakeAlarmRead(userId);
        return Result.success(null);
    }
    

    @ApiOperation("报警信息，断电报警用户，查询站点断电报警情况")
    @GetMapping("/getStationPowerAlarm")
    public Result<Map<String, List<PowerAlarm>>> getStationPowerAlarm(Integer station, String date) {
        return Result.success(powerAlarmService.getPowerAlarmInfo(station,date));
    }

    @ApiOperation(value = "报警信息，断电报警用户，导出断电报警表，导出到Excel")
    @GetMapping("/DownloadPowerAlarm")
    public void DownloadPowerAlarm(String line_name, String date, HttpServletResponse response) throws IOException {

        powerAlarmService.getAlarmExcel(line_name,date,response);
    }

    @ApiOperation("报警信息，超限报警用户，查询站点超限报警情况")
    @GetMapping("/getStationLimitAlarm")
    public Result<Map<String, List<LimitAlarm>>> getStationLimitAlarm(Integer station, String date, String info) {
        return Result.success(limitAlarmService.getLimitAlarmInfo(station,date,info));
    }

    @ApiOperation(value = "报警信息，超限报警用户，超限报警表，导出到Excel")
    @GetMapping("/DownloadLimitAlarm")
    public void DownloadLimitAlarm(String line_name, String date, HttpServletResponse response) throws IOException {

        limitAlarmService.getAlarmExcel(line_name,date,response);
    }
    @ApiOperation("报警信息，直流报警用户，查询站点直流报警情况")
    @GetMapping("/getStationDcAlarm")
    public Result<Map<String, List<DcAlarm>>> getStationDcAlarm(Integer station, String date) {
        return Result.success(dcAlarmService.getDcAlarmInfo(station,date));
    }

    @ApiOperation(value = "报警信息，直流报警用户，直流报警表，导出到Excel")
    @GetMapping("/DownloadDcAlarm")
    public void DownloadDcAlarm(String line_name, String date, HttpServletResponse response) throws IOException {

        dcAlarmService.getAlarmExcel(line_name,date,response);
    }

    @ApiOperation("报警信息，门开报警用户，查询站点门开报警情况")
    @GetMapping("/getStationDoorOpenAlarm")
    public Result<Map<String, List<DoorOpenAlarm>>> getStationDoorOpenAlarm(Integer station, String date) {
        return Result.success(doorOpenAlarmService.getDoorOPenAlarmInfo(station,date));
    }

    @ApiOperation(value = "报警信息，门开报警用户，门开报警表，导出到Excel")
    @GetMapping("/DownloadDoorOPenAlarm")
    public void DownloadDoorOPenAlarm(String line_name, String date, HttpServletResponse response) throws IOException {

        doorOpenAlarmService.getAlarmExcel(line_name,date,response);
    }
    @ApiOperation("报警信息，通讯报警用户，查询站点通讯中断报警情况")
    @GetMapping("/getStationComErrAlarm")
    public Result<Map<String, List<ComErrAlarm>>> getStationComErrAlarm(Integer station, String date) {
        return Result.success(comErrAlarmService.getComErrAlarmInfo(station,date));
    }
    @ApiOperation(value = "报警信息，通讯报警用户，通讯报警表，导出到Excel")
    @GetMapping("/DownloadComErrAlarm")
    public void DownloadComErrAlarm(String line_name, String date, HttpServletResponse response) throws IOException {

        comErrAlarmService.getAlarmExcel(line_name,date,response);
    }
    @ApiOperation("报警信息，仪表组态报警用户，查询站点仪表组态报警情况")
    @GetMapping("/getStationParaAlarm")
    public Result<Map<String, List<ParaAlarm>>> getStationParaAlarm(Integer station, String date) {
        return Result.success(paraAlarmService.getParaAlarmInfo(station,date));
    }

    @ApiOperation(value = "报警信息，仪表组态报警用户，仪表组态报警表，导出到Excel")
    @GetMapping("/DownloadParaAlarm")
    public void DownloadParaAlarm(String line_name, String date, HttpServletResponse response) throws IOException {

        paraAlarmService.getAlarmExcel(line_name,date,response);
    }

    @ApiOperation("报警信息，断电报警用户，查询各线路断电报警情况")
    @GetMapping("/getLinePowerAlarmInfo")
    public Result<Map<String,Object>> getLinePowerAlarmInfo(String line_name, String date, Integer pageNo) {
        IPage<List<AlarmVo>> res = powerAlarmService.getPowerAlarm(line_name,date,pageNo);
        Map<String,Object> map = new HashMap<>();
        map.put("records",res.getRecords());
        map.put("total",res.getTotal());
        map.put("pages",res.getPages());
        return Result.success(map);
    }

    @ApiOperation("报警信息，超限报警用户，查询各线路超限报警情况")
    @GetMapping("/getLineLimitAlarmInfo")
    public Result<Map<String,Object>> getLineLimitAlarmInfo(String line_name, String date, Integer pageNo) {
        IPage<List<LimitAlarmVo>> res = limitAlarmService.getLimitAlarm(line_name,date,pageNo);
        Map<String,Object> map = new HashMap<>();
        map.put("records",res.getRecords());
        map.put("total",res.getTotal());
        map.put("pages",res.getPages());
        return Result.success(map);
    }

    @ApiOperation("报警信息，直流报警用户，查询各线路直流报警情况")
    @GetMapping("/getLineDcAlarmInfo")
    public Result<Map<String,Object>> getLineDcAlarmInfo(String line_name, String date, Integer pageNo) {
        IPage<List<AlarmVo>> res = dcAlarmService.getDcAlarmAlarm(line_name,date,pageNo);
        Map<String,Object> map = new HashMap<>();
        map.put("records",res.getRecords());
        map.put("total",res.getTotal());
        map.put("pages",res.getPages());
        return Result.success(map);
    }

    @ApiOperation("报警信息，门开报警用户，查询各线路门开报警情况")
    @GetMapping("/getLineDoorOpenAlarmInfo")
    public Result<Map<String,Object>> getLineDoorOpenAlarmInfo(String line_name, String date, Integer pageNo) {
        IPage<List<AlarmVo>> res = doorOpenAlarmService.getDoorOpenAlarm(line_name,date,pageNo);
        Map<String,Object> map = new HashMap<>();
        map.put("records",res.getRecords());
        map.put("total",res.getTotal());
        map.put("pages",res.getPages());
        return Result.success(map);
    }

    @ApiOperation("报警信息，通讯报警用户，查询各线路通讯中断报警情况")
    @GetMapping("/getLineComErrAlarmInfo")
    public Result<Map<String,Object>> getLineComErrAlarmInfo(String line_name, String date, Integer pageNo) {
        IPage<List<AlarmVo>> res = comErrAlarmService.getComErrAlarm(line_name,date,pageNo);
        Map<String,Object> map = new HashMap<>();
        map.put("records",res.getRecords());
        map.put("total",res.getTotal());
        map.put("pages",res.getPages());
        return Result.success(map);
    }

    @ApiOperation("报警信息，仪表组态报警用户，查询各线路仪表组态报警情况")
    @GetMapping("/getParaAlarmInfo")
    public Result<Map<String,Object>> getParaAlarmInfo(String line_name, String date, Integer pageNo) {
        IPage<List<AlarmVo>> res = paraAlarmService.getParaAlarm(line_name,date,pageNo);
        Map<String,Object> map = new HashMap<>();
        map.put("records",res.getRecords());
        map.put("total",res.getTotal());
        map.put("pages",res.getPages());
        return Result.success(map);
    }

    @ApiOperation("分页查询管损报警的情况")
    @GetMapping("/getPipelossAlarmInfo")
    public Result<PagedResult> getPipelossAlarmInfo(String lineId, String start, String end, Integer pageNo, Integer pageSize) {
        PagedResult pagedResult = new PagedResult();
        IPage<PipeLossAlarm> res = pipelossAlarmService.getPipelossAlarm(lineId,start,end,pageNo,pageSize);
        pagedResult.setTotal(pagedResult.getTotal());
        pagedResult.setPages(pagedResult.getPages());
        pagedResult.setRows(res.getRecords());
        return Result.success(pagedResult);
    }

    @ApiOperation("(APP)报警通知，报警信息接口")
    @GetMapping("/getAllStationAlarmInfo")
    public Result<List<Map<String,Object>>> getAllStationAlarmInfo(Integer userId) {
        List<Map<String,Object>> resList = new ArrayList<>();
        resList.addAll(comErrAlarmService.polling(userId));
        resList.addAll(dcAlarmService.polling(userId));
        resList.addAll(doorOpenAlarmService.polling(userId));
        resList.addAll(limitAlarmService.polling(userId));
        resList.addAll(paraAlarmService.polling(userId));
        resList.addAll(powerAlarmService.polling(userId));
        return Result.success(resList);
    }

    @ApiOperation("(APP)报警通知，报警信息接口")
    @GetMapping("/getAllStationAlarmInfoByPageNo")
    public Result<List<Map<String,Object>>> getAllStationAlarmInfoByPageNo(Integer userId,Integer pageNo) {
        List<Map<String,Object>> resList = new ArrayList<>();
        resList.addAll(comErrAlarmService.polling(userId));
        resList.addAll(dcAlarmService.polling(userId));
        resList.addAll(doorOpenAlarmService.polling(userId));
        resList.addAll(limitAlarmService.polling(userId));
        resList.addAll(paraAlarmService.polling(userId));
        resList.addAll(powerAlarmService.polling(userId));

        int totalElements = resList.size();

        int totalPages = (int) Math.ceil((double) totalElements / 10);
        if(pageNo > totalPages) throw new MyException(MyExceptiontType.DATAFINISH,null);
        if(pageNo <= 0) pageNo = 1;
        int fromIndex = (pageNo - 1) * 10;
        int toIndex = Math.min(fromIndex + 10, totalElements);

        List<Map<String,Object>> subList = resList.subList(fromIndex, toIndex);
        return Result.success(subList);
    }

    @ApiOperation("(APP)报警通知，部分报警信息接口")
    @GetMapping("/getPartStationAlarmInfo")
    public Result<List<Map<String,Object>>> getPartStationAlarmInfo(Integer userId,String date,String lineName,String type) {
        List<Map<String,Object>> resList = new ArrayList<>();
        switch (type){
            case "通讯报警":{
                resList.addAll(comErrAlarmService.polling(userId,date,lineName));
                break;
            }
            case "直流报警":{
                resList.addAll(dcAlarmService.polling(userId,date,lineName));break;
            }
            case "门开报警":{
                resList.addAll(doorOpenAlarmService.polling(userId,date,lineName));break;
            }
            case "超限报警":{
                resList.addAll(limitAlarmService.polling(userId,date,lineName));break;
            }
            case "仪表组态报警":{
                resList.addAll(paraAlarmService.polling(userId,date,lineName));break;
            }
            case "断电报警":{
                resList.addAll(powerAlarmService.polling(userId,date,lineName));break;
            }
        }
        return Result.success(resList);
    }

}
